package com.nondo.dean.order.service;

import java.util.LinkedHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.nondo.dean.common.constants.GoType;
import com.nondo.dean.common.constants.MessageTips;
import com.nondo.dean.common.constants.MessageType;
import com.nondo.dean.common.web.View;
import com.nondo.dean.order.common.Constant;
import com.nondo.dean.order.domain.OrderSubject;
import com.nondo.dean.order.model.MessageBean;

/**
 * 推送管理业务类
 * 
 * @author zhaoljin
 * @date 2018年5月12日
 */
@Component
public class PushService {

    @Autowired
    private RestTemplate restTemplate;
    
    /**
     * 消息入库并推送给用户
     * 
     * @param message 消息
     * @return
     */
    public void addAndPushMessage(OrderSubject order) {
        MessageBean message = new MessageBean();
        
        message.setToUserId(order.getUserId());
        message.setToUserName(order.getUserName());
        message.setGoValue(String.valueOf(order.getId()));
        
        // 根据审核状态处理
        if (order.getAuditStatus() == Constant.HAVE_BEEN_AUDITED) { // 订单已审核
            message.setTitle(MessageTips.ORDER_AUDIT_SUCESS_TITLE);
            message.setBody(String.format(MessageTips.ORDER_AUDIT_SUCESS_BODY, order.getOrderCode()));
            message.setType(MessageType.ORDER);
            message.setGoType(GoType.OPEN_ORDER_DETAIL);
            sendMessageRequest(message);
            return ;
        }
        
        // 根据订单状态处理
        if(order.getOrderStatus() == Constant.WAITING_TO_PAY){// 待支付订单
            message.setTitle(MessageTips.ORDER_WAITTING_TO_PAY_TITLE);
            message.setBody(String.format(MessageTips.ORDER_WAITTING_TO_PAY_BODY,order.getOrderCode()));
            message.setType(MessageType.ORDER);
            message.setGoType(GoType.OPEN_ORDER_DETAIL);
            sendMessageRequest(message);
        }
        if(order.getOrderStatus() == Constant.WAITING_TO_RECEIVE){// 订单已发货
            message.setTitle(MessageTips.ORDER_SHIPMENTS_SUCESS_TITLE);
            message.setBody(String.format(MessageTips.ORDER_SHIPMENTS_SUCESS_BODY,order.getOrderCode()));
            message.setType(MessageType.ORDER);
            message.setGoType(GoType.OPEN_ORDER_DETAIL);
            sendMessageRequest(message);
        }
        if(order.getOrderStatus() == Constant.HAVE_BEEN_CANCELED){// 已取消订单
            message.setTitle(MessageTips.ORDER_CACEL_SUCCESS_TITLE);
            message.setBody(String.format(MessageTips.ORDER_CACEL_SUCCESS_BODY,order.getOrderCode()));
            message.setType(MessageType.ORDER);
            message.setGoType(GoType.OPEN_ORDER_DETAIL);
            sendMessageRequest(message);
        }
        if(order.getOrderStatus() == Constant.HAVE_BEEN_COMPLETED){// 已收货订单
            message.setTitle(MessageTips.ORDER_HAVE_BEEN_COMPLETED_TITLE);
            message.setBody(String.format(MessageTips.ORDER_HAVE_BEEN_COMPLETED_BODY,order.getOrderCode()));
            message.setType(MessageType.ORDER);
            message.setGoType(GoType.OPEN_ORDER_DETAIL);
            sendMessageRequest(message);
        }
    }

    /**
     * 发送消息请求到消息处理中心
     * 
     * @param message 消息
     * @return 处理结果
     */
    private View<LinkedHashMap<String, Object>> sendMessageRequest(MessageBean message) {
        HttpEntity<MessageBean> httpEntity = new HttpEntity<MessageBean>(message);
        @SuppressWarnings("unchecked")
        View<LinkedHashMap<String, Object>> result =
            restTemplate.postForEntity("http://dean-push/push/v1/inner/message/push/", httpEntity, View.class)
                .getBody();
        return result;
    }

}
